var tools = (function () {
  var toolsObj = {
    // 月份详情
    renderDetailMonth: function (dayWrapper, recivedYear, recivedMonth) {
      var array = []
      var recivedDate = new Date()
      var _html = ``

      var date = new Date(recivedYear, recivedMonth, 1)
      date.setDate(1)
      var week = date.getDay()
      date.setDate(1 - week)
      var month = date.getMonth()

			// 获取日期数据
			function getDateData(date,state){
				return {
					day: date.getDate(),
					lunar: ChineseCalendar.lunarTime(date),
					state: state,
					festival: ChineseCalendar.lunarFestival(date),
					customer: ChineseCalendar.lunarCmFestival(date),
					term: ChineseCalendar.lunarTerm(date),
					dateStr: tools.returnDateStr(date)
				}
			}

			function isWeekEnd(){
				return date.getDay() === 0 || date.getDay() === 6
			}

      for (var i = 0; i < 42; i++) {
        if (month !== recivedMonth) { // 不是当月
          if (isWeekEnd()) {
            array.push(getDateData(date, 'weekend'))
          } else {
            array.push(getDateData(date, ''))
          }
        } else if(tools.curDay(date, recivedDate)) { // 今天
          if (isWeekEnd()) {
            array.push(getDateData(date, 'weekend cur-day'))
          } else {
            array.push(getDateData(date, 'cur-day'))
          }
        } else { // 当月且不是今天
          if (isWeekEnd()) {
            array.push(getDateData(date, 'weekend cur-month'))
          } else {
            array.push(getDateData(date, 'cur-month'))
          }
        }

        date.setDate(date.getDate() + 1)
        month = date.getMonth()
      }

      for (var j = 0; j < array.length; j++) {
        _html += `<li data-time="${array[j].dateStr}" class="${array[j].state}">
                    <p class="info">
                      <span class="date"><em>${array[j].day}</em></span>
                    </p>
										${getDateList(array[j])}
										<div class="showAllList" data-time="${array[j].dateStr}">
											${getDateAllList(array[j])}
											</div>
                  </li>`
      }
			
			//事件备注渲染
			function getDateAllList(obj){
				var festivals = [];
				var festivalDescs = [];
				var dateStr = formatDateStr(obj.dateStr);
				var festival = obj.festival;
				var term = obj.term;
				var customer = obj.customer;
				var termDesc, customerDesc;
				var dateFormat = tools.strFormatDate(obj.dateStr)
      	var lunarObj = ChineseCalendar.date2lunar(dateFormat);
				var {
					month, day,
					lunarMonth, lunarDay,
				} = lunarObj;

				var cmd = `${padding(month)}${padding(day)}`
				var lmd = `${padding(lunarMonth)}${padding(lunarDay)}`

				console.log(obj);
				if(festival){
					// 节日描述

					// 农历节日
					if(lFestive[lmd]){
						festivalDescs.push(lFestive[lmd].desc);
					}

					// 公历节日
					if(cFestive[cmd]){
						festivalDescs.push(cFestive[cmd].desc);
					}

					// 星期节日
					var wf = festival.filter(getWFestivalDesc);

					if(wf[0]){
						festivalDescs.push(getWFestivalDesc(wf[0]));
					}

					festivals = festival.map(function(item, index){
						return {
							className:'festival',
							name: item,
							desc: festivalDescs.shift(),  //shift() 方法用于把数组的第一个元素从其中删除，并返回第一个元素的值。
						}
					})
					
				}

				// 节气描述
				if(term){
					for(var i = 0, l = solarTerm.length; i < l; i++){
						if(solarTerm[i].name === term){
							termDesc = solarTerm[i].desc;
							break;
						}
					}
				}

				// 自定义日期描述
				if(cmFestive[dateStr]){
					customerDesc = cmFestive[dateStr].desc;
				}

				var arr = [
					...festivals,
					{className:'term',name:term,desc:termDesc},
					{className:'customer',name:customer, desc: customerDesc}
				];
				var list = '';
				var listArr = [];
				var listArrLh = 0;

				for(var i = 0,l = arr.length;i < l;i++){
					if(arr[i].name){
						var desc = arr[i].desc;

						list += `<section class="${arr[i].className} fesBox ${arr[i].name ? 'show' : ''}">
						<h3>${arr[i].name}</h3>
						<p style='display: ${desc ? "block": "none"};'>${desc}</p>
						</section>`
					}
				}
				
				//根据p标签分割list，并放入数组
				listArr = list.split('</section>');
				listArrLh = listArr.length;
				
				if(listArrLh > 0){
					listArr.splice(-1,1);
					listArrLh = listArr.length;
					for(i=0;i<listArrLh;i++){
						listArr[i] += '</section>';
					}
				}
			
				$(".rkNum").html(listArr.length);
			
				list = listArr.join("");

				return list; 
			}
			
			//把每一天的节日名称标签显示出来
			function getDateList(obj){
				var festival = obj.festival;
				var term = obj.term;
				var customer = obj.customer;
				var arr = [{className:'festival',name:festival},{className:'term',name:term},{className:'customer',name:customer}];
				var list = '';
				var listArr = [];
				var listArrLh = 0;
				
				for(var i = 0,l = arr.length;i < l;i++){
					if(arr[i].name){
						list += `<p class="${arr[i].className} ${arr[i].name ? 'show' : ''}">${arr[i].name}</p>`
					}
				}				
				
				//1.根据p标签分割list，并放入数组
				//2.根据新数组的长度，超出隐藏
				listArr = list.split('</p>');
				listArrLh = listArr.length;
				
				if(listArrLh > 0){
					listArr.splice(-1,1);
					listArrLh = listArr.length;
					for(i=0;i<listArrLh;i++){
						listArr[i] += '</p>';
					}
				}
				
					list = listArr.join("");
				return list; 
			}
			
      dayWrapper.innerHTML = _html
    },

    nowDate: function () {
      return new Date()
    },

    returnDateStr: function (date) {
      var year = date.getFullYear();
      var month = date.getMonth();
      var day = date.getDate();

      month = month <= 9 ? ('0' + month) : ('' + month);
      day = day <= 9 ? ('0' + day) : ('' + day);

      return year + month + day;
    },

    // 是否是当天
    curDay: function (oldTime, nowTime) {
      return oldTime.getFullYear() === nowTime.getFullYear() &&
             oldTime.getMonth() === nowTime.getMonth() &&
             oldTime.getDate() === nowTime.getDate()
    },

    // 格式化日期 (如果这里传入的是字符串，那么得到的值不会按照下标去算)
    strFormatDate: function (str) {
			// console.log(str);  //20190027  20190104
      var date = new Date(parseInt(str.substr(0, 4)), parseInt(str.substr(4, 2)), parseInt(str.substr(6)))
			
      return date
    }
  }

  return toolsObj
}())

/* 
	参数为日期
	* @method 月份+1
	* @param {string} 年月日
	* @return {boolean} 返回月份+1后的年月日
*/
function formatDateStr(str){
	var yyyy, mm, dd;
	
	yyyy = str.substr(0, 4);
	mm = str.substr(4, 2);
	dd = str.substr(-2);
	return yyyy + padding(+mm + 1) + dd;
}